"use strict"

const Service = require('egg').Service;

class articleTable extends Service{
    async getArticleList(){
        try {
            
            let sql = 'SELECT article.id as id,'+
            'article.title as title,'+
            'article.introduce as introduce,'+
            "FROM_UNIXTIME(article.addTime,'%Y-%m-%d %H:%i:%s') as addTime,"+
            'article.view_count as view_count ,'+
            '.type.typeName as typeName '+
            'FROM article LEFT JOIN type ON article.type_id = type.Id'
            const app = this.app
            const res = await app.mysql.query(sql)
            return res
        } catch (error) {
            console.log(error)
            return null
        }
    }

    async getAriticleById(id){
        try {

            let sql = 'SELECT article.id as id,'+
            'article.title as title,'+
            'article.introduce as introduce,'+
            'article.article_content as article_content,'+
            "FROM_UNIXTIME(article.addTime,'%Y-%m-%d %H:%i:%s' ) as addTime,"+
            'article.view_count as view_count ,'+
            'type.typeName as typeName ,'+
            'type.id as typeId '+
            'FROM article LEFT JOIN type ON article.type_id = type.Id '+
            'WHERE article.id='+id
            const app = this.app
            const res = await app.mysql.query(sql)
            return res
        } catch (error) {
            console.log(error)
            return null
        }
    }

    async getTypeInfo(){
        try {
            const app = this.app
            const res = await this.app.mysql.select('type')
            return res
        } catch (error) {
            console.log(error)
            return null
        }
    }

    //根据文字类别id 获取列表
    async getListById(id){
        try {
            let sql = 'SELECT article.id as id,'+
            'article.title as title,'+
            'article.introduce as introduce,'+
            "FROM_UNIXTIME(article.addTime,'%Y-%m-%d %H:%i:%s' ) as addTime,"+
            'article.view_count as view_count ,'+
            'type.typeName as typeName '+
            'FROM article LEFT JOIN type ON article.type_id = type.Id '+
            'WHERE type_id='+id
        const app = this.app
        const res = await app.mysql.query(sql)
        return res
        } catch (error) {
          console.log(error)
          return null

        }
    }

}

module.exports = articleTable;